﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Power.Control
{
    /// <summary>
    /// author:TT
    /// time:2021/9/1 11:20:32
    /// desc:ValueCheckHelper
    /// </summary>
    public class ValueCheckHelper
    {
        public static readonly DependencyProperty IsNullValueProperty = DependencyProperty.RegisterAttached("IsNullValue", typeof(bool), typeof(ValueCheckHelper), new PropertyMetadata(true, IsNullValueChanged));

        private static void IsNullValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d == null)
                return;
            TextBox textBox = d as TextBox;
            if (textBox != null)
            {
                if ((bool)e.NewValue == false)
                {
                    textBox.BorderBrush = Brushes.Red;
                    textBox.BorderThickness = new Thickness(1);
                }
                else
                {
                    textBox.BorderBrush = Brushes.Black;
                    textBox.BorderThickness = new Thickness(0);
                }
            }
            else
            {
                DatePicker datePicker = d as DatePicker;
                if (datePicker != null)
                {
                    if ((bool)e.NewValue == false)
                    {
                        datePicker.BorderBrush = Brushes.Red;
                        datePicker.BorderThickness = new Thickness(1);
                    }
                    else
                    {
                        datePicker.BorderBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFAFB9C1"));
                        //datePicker.BorderThickness = new Thickness(0);
                    }
                }
            }
        }

        public static bool GetIsNullValue(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsNullValueProperty);
        }

        public static void SetIsNullValue(DependencyObject obj, bool value)
        {
            obj.SetValue(IsNullValueProperty, value);
        }
    }
}
